package com.egao.common.module.system.transfer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.egao.common.core.web.JsonResult;
import com.egao.common.module.guidance.entity.Lecture;
import com.egao.common.module.guidance.entity.LectureApply;
import com.egao.common.module.guidance.mapper.LectureApplyMapper;
import com.egao.common.module.guidance.mapper.LectureMapper;
import com.egao.common.module.guidance.service.LectureApplyService;
import com.egao.common.module.guidance.service.LectureService;
import com.egao.common.module.student.entity.Student;
import com.egao.common.module.student.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.*;
import java.util.List;

/**
 * 讲座数据迁移
 * @author pyx
 */
@Api(tags = "讲座数据迁移")
@RestController
@Slf4j
public class lectureDataController {
    @Value("${old.datasource.jyurl}")
    private String url;

    @Value("${old.datasource.username}")
    private String userName;

    @Value("${old.datasource.password}")
    private String password;

    @Value("${minio.old-data-head}")
    private String contentUrl;

    @Autowired
    private LectureService lectureService;
    @Autowired
    private LectureApplyService lectureApplyService;
    @Autowired
    private StudentService studentService;
    @Autowired(required = false)
    private LectureApplyMapper lectureApplyMapper;
    @Autowired(required = false)
    private LectureMapper lectureMapper;


    @ApiOperation("讲座表")
    public JsonResult lecture(){
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(url, userName, password);
            st = conn.createStatement();
            String sql = "SELECT * FROM employmentlecture";
            rs = st.executeQuery(sql);

            while (rs.next()){
                Lecture lecture = new Lecture();
                lecture.setOldId(rs.getString("id"));
                lecture.setTitle(StringEscapeUtils.unescapeHtml4(rs.getString("title")));
                String comment = rs.getString("comment");
                lecture.setContent(comment != null ? comment.replaceAll("/ueditor/jsp/upload", contentUrl + "/ueditor/jsp/upload") : "");
                lecture.setStartTime(rs.getDate("holdtime"));
                lecture.setEndTime(rs.getDate("endtime"));
                lecture.setApplyEndTime(rs.getDate("downtime"));
                lecture.setHoldAddress(rs.getString("situs"));
                lecture.setCreateTime(rs.getDate("update_date"));

                String num = rs.getString("limitnumber");
                Integer applyNum = Integer.valueOf(num);
                lecture.setApplyNum(applyNum);

                String delFlag = rs.getString("del_flag");
                int del = 0;
                if("1".equals(delFlag)){
                    del = 1;
                }
                lecture.setDeleted(del);

                lectureService.save(lecture);

            }

        }catch (ClassNotFoundException | SQLException e){
            e.printStackTrace();
        }finally {
            try {
                rs.close();
                st.close();
                conn.close();
                return JsonResult.ok();
            }catch (SQLException e){
                e.printStackTrace();
                return JsonResult.error();
            }
        }
    }

    @ApiOperation("讲座报名表")
    @GetMapping("/publicly/transfer/lecture/apply")
    public JsonResult apply(){
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(url, userName, password);
            st = conn.createStatement();
            String sql = "SELECT *,s.loginname AS xh FROM lecturereg l LEFT JOIN student s ON l.studentid = s.id WHERE s.loginname IS NOT NULL";
            rs = st.executeQuery(sql);

            while(rs.next()){
                LectureApply lectureApply = new LectureApply();
                lectureApply.setOldId(rs.getString("id"));
                String xh = rs.getString("xh");
                Student student = studentService.getByXh(xh);
                if(student == null){
                    continue;
                }
                lectureApply.setStudentId(student.getId());
                lectureApply.setState(rs.getInt("checkstate"));
                if(rs.getInt("iscancel") == 1){
                    lectureApply.setState(3);
                }
                lectureApply.setOldLecture(rs.getString("employmentlectureid"));
                lectureApply.setCreateTime(rs.getDate("create_date"));
                lectureApply.setUpdateTime(rs.getDate("update_date"));

                String delFlag = rs.getString("del_flag");
                int del = 0;
                if("1".equals(delFlag)){
                    del = 1;
                }
                lectureApply.setDeleted(del);

                lectureApplyService.save(lectureApply);
            }

        }catch (ClassNotFoundException | SQLException e){
            e.printStackTrace();
        }finally {
            try {
                rs.close();
                st.close();
                conn.close();
                lectureApply();
                return JsonResult.ok();
            }catch (SQLException e){
                e.printStackTrace();
                return JsonResult.error();
            }
        }
    }

    //原数据讲座id处理
    private void lectureApply(){
        List<LectureApply> applyList = lectureApplyMapper.selectList(null);
        for (LectureApply apply:applyList) {
            String oldLecture = apply.getOldLecture();
            QueryWrapper<Lecture> wrapper = new QueryWrapper<>();
            wrapper.eq("old_id",oldLecture);
            Lecture lecture = lectureMapper.selectOne(wrapper);
            apply.setLectureId(lecture.getId());
        }

        lectureApplyService.updateBatchById(applyList);
    }
}
